package com.clear.command.demo2;

import java.util.Optional;
import java.util.Stack;

/**
 * 请求者：文本编辑器
 */
public class TextEditor {
    private StringBuilder text;
    private Stack<Command> commandHistory;

    public TextEditor() {
        this.text = new StringBuilder();
        this.commandHistory = new Stack<>();
    }

    public void addText(String newText) {
        Command command = new AddTextCommand(text, newText);
        command.execute();
        commandHistory.push(command);
    }

    public void undo() {
        if (!commandHistory.isEmpty()) {
            Command lastCommand = commandHistory.pop();
            lastCommand.undo();
        }
    }

    public void printContent() {
        System.out.println(text.toString());
    }
}
